﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class FXBGanalyse : System.Web.UI.Page
{
    string tableName = "DATA" + Convert.ToString(DateTime.Now.Year);
    string Month = Convert.ToString(DateTime.Now.Month);
    string fileType = HttpContext.Current.Request.QueryString["ft"];
    string fileTypeName = HttpContext.Current.Request.QueryString["ftn"];
    string Year = DateTime.Now.Year.ToString();
    DBOperation db = new DBOperation();
    int AllDepCount = 0;
    int AllDoneCount = 0;
    int NoDoneAllCount = 0;
    int roleID = 2;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] != null)
        {
            if (!IsPostBack)
            {
                BindAnalyseDate();
                lblMsg.Text =fileTypeName+" "+ Month;
                BindYear();
                roleID = Convert.ToInt32(Session["roleID"]);
                if (roleID == 1)
                {
                    btnExport.Visible = true;
                }
            }
        }
        else
        {
            Response.Redirect("Longin.aspx");
        }


    }
    //绑定统计数据
    protected void BindAnalyseDate()
    {
        Department dp = new Department();
        dgResult.DataSource = dp.FindDep();
        dgResult.DataBind();
    }
    //统计方法
    protected void dgResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {

        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:

                string strSqlNoDone = "select count(*) from data" + Year + " as data inner join lc" + Year + " as lc on data.dataid=lc.dataid where lc.status='0' and data.depid= " + Convert.ToInt32(e.Item.Cells[0].Text) + " and data.flowtypemark='" + fileType + "'and MONTH(lc.BEGINDATE)="+Month;
                int NoFinishCount = Convert.ToInt32(db.ExcuteScalar(strSqlNoDone));
                Label lblNoFinishCount = (Label)e.Item.Cells[4].FindControl("lblNoFinishCount");
                lblNoFinishCount.Text = NoFinishCount.ToString();

                string strSqlDone = "select count(*) from data" + Year + " as data inner join lc" + Year + " as lc on data.dataid=lc.dataid where lc.status='1' and data.depid= " + Convert.ToInt32(e.Item.Cells[0].Text) + " and data.flowtypemark='" + fileType + "'and MONTH(lc.BEGINDATE)=" + Month;
                int countDone = Convert.ToInt32(db.ExcuteScalar(strSqlDone));
                Label lblDoneCount = (Label)e.Item.Cells[3].FindControl("lblDoneCount");
                lblDoneCount.Text = countDone.ToString();

                int allCount = NoFinishCount + countDone;
                Label lblAllCount = (Label)e.Item.Cells[2].FindControl("lblAllCount");
                lblAllCount.Text =allCount.ToString();






                AllDepCount = AllDepCount + allCount;
                AllDoneCount = AllDoneCount + countDone;
                NoDoneAllCount = NoDoneAllCount + NoFinishCount;

                break;
            case ListItemType.Footer:
                Label lblAllDepCount = (Label)e.Item.Cells[2].FindControl("lblAllDepCount");
                Label lblAllDoneCount = (Label)e.Item.Cells[3].FindControl("lblAllDoneCount");
                Label lblNoFinishAllCount = (Label)e.Item.Cells[4].FindControl("lblNoFinishAllCount");

                lblAllDepCount.Text = lblAllDepCount.Text + AllDepCount.ToString();
                lblAllDoneCount.Text = lblAllDoneCount.Text + AllDoneCount.ToString();
                lblNoFinishAllCount.Text = lblNoFinishAllCount.Text + NoDoneAllCount.ToString();
                break;

        }

    }
    //该重写方法不可以删除！！！！！！！
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    //导出excel
    protected void btnExport_Click(object sender, EventArgs e)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        dgResult.RenderControl(hw);

        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        Page.EnableViewState = false;

        Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileTypeName, System.Text.Encoding.UTF8) + ".xls");
        Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html\"></head><body><center>");
        Response.Write(sw.ToString());
        Response.Write("</center></body></html>");
        Response.End();
    }
    //选择月份查询
    protected void ddlMoth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Month = ddlMoth.SelectedValue;
        lblMsg.Text = fileTypeName + " " + Month;
        Year = ddlYear.SelectedItem.Text;
        BindAnalyseDate();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("DataSearch.aspx");
    }
    //绑定年份
    public void BindYear()
    {
        string strSelYear = "select DATAYEAR from DATAYEAR order by AUTOID desc";
        SqlDataReader sdr = db.GetDataReader(strSelYear);
        ddlYear.DataSource = sdr;
        ddlYear.DataTextField = "DATAYEAR";
        ddlYear.DataBind();
        sdr.Close();
    }
}
